#include <basic/errcode.h>
#include <basic/dbc.h>

void dbc_query_free(DBC_RSET *rset)
{/*{{{*/
    int row_idx, col_idx;

    if(!rset || !rset->rows)
        return;

    for(row_idx=0; row_idx<rset->row_num; row_idx++)
    {
        for(col_idx=0; col_idx<rset->col_num; col_idx++)
        {
            FREE(rset->rows[row_idx][col_idx]);
        }
        FREE(rset->rows[row_idx]);
    }
    FREE(rset->rows);
    rset->col_num=0;
    rset->row_num=0;
    rset->row_alloc_num=0;

    return;
}/*}}}*/

int dbc_bind_char(DBC_BIND *bind, unsigned int idx, char *var,
        unsigned int col_size)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, col_size+1,
            RET_ERR_ALLOC);
    memset(bind->vars[idx], SPACE, col_size);
    bind->vars[idx][col_size]=ZERO;
    if(var)
    {
        int var_size=strlen(var);
        strncpy(bind->vars[idx], var,
                (var_size <= col_size)? var_size : col_size);
    }
    return(0);
}/*}}}*/

int dbc_bind_varchar(DBC_BIND *bind, unsigned int idx, char *var)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, ((var)?strlen(var):0)+2,
            RET_ERR_ALLOC);
    if(var)
    {
        strcpy(bind->vars[idx], var);
        trim_tail_space_notnull(bind->vars[idx]);
    }
    else
    {
        bind->vars[idx][0]=SPACE;
        bind->vars[idx][1]=ZERO;
    }
    return(0);
}/*}}}*/

int dbc_bind_int(DBC_BIND *bind, unsigned int idx, int var)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, 20, RET_ERR_ALLOC);
    sprintf(bind->vars[idx], "%d", var);
    return(0);
}/*}}}*/

int dbc_bind_long(DBC_BIND *bind, unsigned int idx, long var)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, 30, RET_ERR_ALLOC);
    sprintf(bind->vars[idx], "%ld", var);
    return(0);
}/*}}}*/

int dbc_bind_int64(DBC_BIND *bind, unsigned int idx, int64_t var)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, 30, RET_ERR_ALLOC);
    sprintf(bind->vars[idx], "%lld", (long long int)var);
    return(0);
}/*}}}*/

int dbc_bind_double(DBC_BIND *bind, unsigned int idx, double var)
{/*{{{*/
    ALLOC_MULTI_RETERR(bind->vars[idx], char, 30, RET_ERR_ALLOC);
    sprintf(bind->vars[idx], "%lf", var);
    return(0);
}/*}}}*/

char *dbc_col_ref(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    char *p=rset->rows[row_idx][col_idx];
    trim_tail_space_notnull(p);
    return(p);
}/*}}}*/

char *dbc_col_export_char(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx, char *host)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    if(host) strcpy(host, rset->rows[row_idx][col_idx]);
    return(rset->rows[row_idx][col_idx]);
}/*}}}*/

char *dbc_col_dup_char(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    return(strdup(rset->rows[row_idx][col_idx]));
}/*}}}*/

char *dbc_col_take_char(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    char *p=rset->rows[row_idx][col_idx];
    rset->rows[row_idx][col_idx]=NULL;
    trim_tail_space_notnull(p);
    return(p);
}/*}}}*/

int dbc_col_export_int(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    return(atoi(rset->rows[row_idx][col_idx]));
}/*}}}*/

long dbc_col_export_long(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    return(atol(rset->rows[row_idx][col_idx]));
}/*}}}*/

int64_t dbc_col_export_int64(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    return(atoll(rset->rows[row_idx][col_idx]));
}/*}}}*/

double dbc_col_export_double(DBC_RSET *rset,
        unsigned int row_idx, unsigned int col_idx)
{/*{{{*/
    trim_tail_space_notnull(rset->rows[row_idx][col_idx]);
    return(atof(rset->rows[row_idx][col_idx]));
}/*}}}*/
